package gptma.bo.dao.project;

import gptma.bo.dao.GenericDaoJpaImpl;
import gptma.bo.model.project.Project;
import gptma.bo.model.request.ClientRequest;
import gptma.bo.model.task.Task;

import java.util.List;

public class ProjectDaoImpl extends GenericDaoJpaImpl<Project, Long> implements
		ProjectDao {

	@Override
	@SuppressWarnings("unchecked")
	public List<String> completeProjectName(String partialName) {
		String wildcard = new StringBuilder("%").append(partialName)
				.append("%").toString();
		return em.createNamedQuery("Project.completeName")
				.setParameter("name", wildcard).getResultList();
	}

	@Override
	public Project findByName(String name) {
		return (Project) em.createNamedQuery("Project.byName")
				.setParameter("name", name).getSingleResult();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Project> findProjectsByName(String name) {
		return em.createNamedQuery("Project.byName").setParameter("name", name)
				.getResultList();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Task> findDirectlyAttachedTasks(Project project) {
		return em.createNamedQuery("Project.findTasks")
				.setParameter("project", project).getResultList();
	}

	@Override
	public int findRequestCount(Project project) {
		return ((Long) em.createNamedQuery("Project.requestCount")
				.setParameter("project", project).getSingleResult()).intValue();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ClientRequest> findRequests(Project project) {
		return em.createNamedQuery("Project.findRequests")
				.setParameter("project", project).getResultList();
	}

}
